let arrList = [
  { uname: "zs", pwd: "1323" },
  { uname: "ls", pwd: "465" },
];

exports.getQueryList = (ctx) => {
  ctx.status = 200;
  ctx.body = {
    msg: "获取列表成功",
    data: arrList,
  };
};

exports.getSingleQueryList = (ctx) => {
  try {
    let id = ctx.params.id * 1;
    if (id > arrList.length) ctx.throw("找不到");

    let curVal = arrList.find((v, i) => {
      if (i == id) {
        return v;
      }
    });
    ctx.body = {
      msg: "获取列表成功",
      data: curVal,
    };
  } catch (error) {
    ctx.throw(eror);
  }
};

exports.getAddList = (ctx) => {
  ctx.verifyParams({
    uname: {
      type: "string",
      required: true,
    },
    pwd: {
      type: "number",
      require: true,
    },
  });
  arrList.push(ctx.request.body);
  ctx.body = {
    msg: "success",
    data: arrList,
  };
};

exports.getDeleteList = (ctx) => {
  let id = ctx.params.id * 1;
  let newArr = arrList.filter((v, i) => i != id);
  ctx.body = {
    msg: "success",
    data: newArr,
  };
};

exports.getUpdateList = (ctx) => {
  let { uname, pwd, id } = ctx.request.body;
  let newArr = arrList.map((v, i) => {
    if (i == id) {
      return (v = { uname, pwd });
    }
    return v;
  });
  ctx.body = {
    msg: "success",
    data: newArr,
  };
};
